package com.usion.student.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.ResultVo;
import com.usion.student.dao.StudeDao;
import com.usion.student.feign.ClasssFeignService;
import com.usion.student.feign.LoginFeignService;
import com.usion.student.pojo.Classs;
import com.usion.student.pojo.Stude;
import com.usion.student.pojo.User;
import com.usion.student.service.StudeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;


@Service
public class StudeServiceImpl extends ServiceImpl<StudeDao, Stude> implements StudeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private StudeDao studeDao;
    @Autowired
    private ClasssFeignService classsFeignService;
    @Autowired
    private LoginFeignService loginFeignService;
    @Override
    public ResultVo regin(Stude stude)throws  Exception {
//        VcommonApi vcommonApi=new VcommonApi();
//
//      Map resut=vcommonApi.callapi(stude.getCardNo(),stude.getPhone() ,stude.getName() );

      if (true){
          String isPhone =  stringRedisTemplate.opsForValue().get(stude.getPhone());
               /*
               * 判断验证码是否过期*/
          if (!StringUtils.isEmpty(isPhone)){
              /*
               * 判断验证码是否与redis中相等*/
          if (isPhone.equals(stude.getCode())){
              /*
               * 查询用户是否注册*/
              LambdaQueryWrapper<Stude> lambdaQueryWrapper=new LambdaQueryWrapper<Stude>();
              lambdaQueryWrapper.eq(Stude::getPhone,stude.getPhone() );
             int count=studeDao.selectCount(lambdaQueryWrapper);
             if (count==0){
               ResultVo resultVo=  classsFeignService.info(stude.getClasssId());
               Classs stude1= JSON.parseObject(JSON.toJSONString(resultVo.getData()), Classs.class );
                 System.out.println(stude1);
               stude.setHId(stude1.getHid());
               stude.setTId(stude1.getTid());
                 studeDao.insert(stude);

                 User user = new User(stude.getPhone(),stude.getName(),5);

                 loginFeignService.add(user);
                 return new ResultVo(200,"success",null);
             }else {


                 return new ResultVo(202,"用户已经存在",null);
             }
          }else {
              return new ResultVo(202,"验证码错误",null);
          }
          }else {
              return new ResultVo(1,"错误",null);
          }
      }else {
      }
          return new ResultVo();
    }
}
